﻿CREATE proc [dbo].[proc_Product_Create]
(
	@Sort int,
	@CateOneId int,
	@CateTwoId int,
	@SupplierId int,
	@Title nvarchar(50),
	@CostPrice numeric(18,2),
	@SalePrice numeric(18,2),
	@Description nvarchar(1000),
	@UpTime dateTime,
	@SCateOneId int,
	@SCateTwoId int,
	@SubjectId INT,
	@xmxf int
	,@flag int 
)
As
Begin
	Insert into Product (Sort,CateOneId,CateTwoId,SupplierId,Title,CostPrice,SalePrice,Description,UpTime,SCateOneId,SCateTwoId,SubjectId,xmfx,flag) 
	Values(@Sort,@CateOneId,@CateTwoId,@SupplierId,@Title,@CostPrice,@SalePrice,@Description,@UpTime,@SCateOneId,@SCateTwoId,@SubjectId,@xmxf,@flag)

	If @xmxf=1
	Begin	--成单专用的产品，不需要设定接口人，项目分析会分析成其他产品 By ZHT 2014-06-20
		--插入接口人表
		Declare @temptable table(id int identity(1,1),cid int)
		Insert Into @temptable(cid) select distinct companyid from ProjectInterfaceSet

		Declare @i int,@productid int,@companyid int,@rcount int
		Set @i=1
		Set @productid=(select top 1 id from Product order by id desc)
		Set @rcount = (select count(Id) From @temptable)
		While(@i<=@rcount)
		Begin 
			Set @companyid = (Select cid From @temptable where id=@i)
			Insert Into ProjectInterfaceSet (ProductId,CId,CateOneId,CateTwoId,Title,CostPrice,SalePrice,Description,Sort,CompanyId) 
			Values(@productid,0,@CateOneId,@CateTwoId,@Title,@CostPrice,@SalePrice,@Description,@Sort,@companyid)
			Set @i=@i+1
		End
	End
End